//
//  TopicCollectionViewCell.swift
//  appstore
//
//  Created by llbt-sk on 2019/11/7.
//  Copyright © 2019 Roy. All rights reserved.
//

import UIKit

class TopicCollectionViewCell: UICollectionViewCell {
    
    var iconImageView: UIImageView!
    var nameLabel: UILabel!
    var descLabel: UILabel!
    var lineView: UIView!
    var updateButton:UIButton!
    
    var updateClick:((_ button:UIButton) -> (Void))?
    
    var model: GameTopicModel! {
        didSet {
            iconImageView.image = UIImage(named: model.iconImageName)
            nameLabel.text = model.name
            descLabel.text = model.desc
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initView()
    }
    
    func initView() -> Void {
        
        iconImageView = UIImageView()
        self.contentView.addSubview(iconImageView)
        iconImageView.snp.makeConstraints { (make) in
            make.top.equalTo(10.0)
            make.left.equalTo(0.0)
            make.width.height.equalTo(60.0)
        }
        
        nameLabel = UILabel()
        nameLabel.font = UIFont.systemFont(ofSize: 17.0)
        self.contentView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconImageView.snp.top).offset(10.0)
            make.left.equalTo(iconImageView.snp.right).offset(5.0)
            make.width.equalTo(150.0)
            make.height.equalTo(20.0)
        }
        
        descLabel = UILabel()
        descLabel.font = UIFont.systemFont(ofSize: 15.0)
        descLabel.textColor = UIColor.gray
        self.contentView.addSubview(descLabel)
        descLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(5.0)
            make.left.equalTo(iconImageView.snp.right).offset(5.0)
            make.width.equalTo(150.0)
            make.height.equalTo(20.0)
        }
        
        updateButton = UIButton(type: .custom)
        updateButton.setTitle("Update", for: .normal)
        updateButton.backgroundColor = .gray
        updateButton.setTitleColor(.blue, for: .normal)
        updateButton.addTarget(self, action: #selector(updateClickAtion(button:)), for: .touchUpInside)
        self.contentView.addSubview(updateButton)
        updateButton.snp.makeConstraints { (make) in
            make.right.equalTo(0.0)
            make.width.equalTo(80.0)
            make.height.equalTo(30.0)
            make.centerY.equalTo(contentView)
        }
    }
    
    @objc func updateClickAtion(button:UIButton) -> Void {
        updateClick!(button)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
